﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Collections.Generic;

namespace ProjectDan.Shell.Views
{
	public partial class Administration : Microsoft.Practices.CompositeWeb.Web.UI.Page, IAdministrationView
	{
		private AdministrationPresenter _presenter;
        private Resources resources;
        private string currentPopup = "";

		protected void Page_Load(object sender, EventArgs e)
		{
            // Instantiate resources
            if (this.resources == null)
            {
                this.resources = new Resources();
                addDictionaryValues();
            }
            
            if (!this.IsPostBack)
			{
				this._presenter.OnViewInitialized();

                //this.lbxPopups.SelectedIndexChanged += new EventHandler(lbxPopups_SelectedIndexChanged);
                this.btnUpdate.Click += new EventHandler(btnUpdate_Click);
			}
			this._presenter.OnViewLoaded();

            // Fill list box
            //this.lbxPopups.Items.Clear();
            //foreach (KeyValuePair<string, DescriptionPopup> kvp in this.resources.HelpMessages)
            //{
            //    this.lbxPopups.Items.Add(kvp.Key);
            //}

            //test
            //this.txtError.Text = this.resources.getPopups()["BMI"].Header;
		}

        private void addDictionaryValues()
        {
            //Response.Write("<form runat='server' id='valueForm'>");
            foreach (KeyValuePair<string, DescriptionPopup> kvp in this.resources.HelpMessages)
            {
                Response.Write("<input id=\"" + kvp.Key + "_head\" value=\"" + kvp.Value.Header + "\" type=\"hidden\" />");
                Response.Write("<input id=\"" + kvp.Key + "_descr\" value=\"" + kvp.Value.Description + "\" type=\"hidden\" />");
            }
            //Response.Write("</form>");
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            resources.HelpMessages[Request.Form["txtKey"]].Header = Request.Form["txtHeader"];
            resources.HelpMessages[Request.Form["txtKey"]].Description = Request.Form["txtDescription"];
            //if (this.txtHeader.Text != "" && this.txtDescription.Text != "")
            //{
            //    this.resources.getPopups()[this.txtHeader.Text].Header = this.txtHeader.Text;
            //    this.resources.getPopups()[this.txtHeader.Text].Description = this.txtDescription.Text;
            //}
        }

        
        //void lbxPopups_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (this.lbxPopups.SelectedIndex >= 0)
        //    {
        //        this.txtError.Text = this.lbxPopups.SelectedItem.Value;
        //        this.currentPopup = this.resources.HelpMessages[this.lbxPopups.SelectedValue].Header;
        //        this.txtHeader.Text = this.resources.HelpMessages[this.lbxPopups.SelectedValue].Header;
        //        this.txtDescription.Text = this.resources.HelpMessages[this.lbxPopups.SelectedValue].Description;
        //    }
        //}

		[CreateNew]
		public AdministrationPresenter Presenter
		{
			get
			{
				return this._presenter;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException("value");

				this._presenter = value;
				this._presenter.View = this;
			}
		}

		// TODO: Forward events to the presenter and show state to the user.
		// For examples of this, see the View-Presenter (with Application Controller) QuickStart:
		//	

	}
}

